//
//  LXApplyAnnexTableCell.h
//  ZuLinProject
//
//  Created by bancheng on 2021/12/24.
//  Copyright © 2021 LX_App. All rights reserved.
//
#import "LXApplyAnnexTableCell.h"

#import "LXApplyPicCollectionCell.h"


@interface LXApplyAnnexTableCell ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic) UICollectionView *collectionView;

@property (nonatomic) NSMutableArray *dataSource;

@property (nonatomic) UIView *maskView;

@end


#define kItemHeight (KHeightScale(115))

#define kItemCount 3

static NSInteger kNum = 0;

@implementation LXApplyAnnexTableCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self configUI];
        
    }
    return self;
}

+(CGFloat)getCellHeightWithDataSource:(NSMutableArray *)dataSource
{
    
    // 如果数据的个数大于指定的张数
    NSMutableArray *tempAry = [NSMutableArray arrayWithArray:dataSource].mutableCopy;
    if (tempAry.count > kNum) {
        [tempAry removeLastObject];
    }
    CGFloat height = tempAry.count/kItemCount *kItemHeight;
    if (tempAry.count%kItemCount !=0 ){
        height = (tempAry.count/kItemCount +1) *kItemHeight ;
    }
    
    return height;
}

- (void)reloadAnnexData:(NSMutableArray *)dataSource WithMaxNum:(NSInteger)num{
    _dataSource = dataSource;
    
    kNum = num;
    CGFloat height = [LXApplyAnnexTableCell getCellHeightWithDataSource:dataSource];
    self.collectionView.frame = CGRectMake(0, 30, kScreenWidth, height);
    [self.collectionView reloadData];
    if (!StringIsEmpty(self.subLab.text)) {
        self.subLab.frame=  CGRectMake(15, MaxY(self.collectionView), kScreenWidth - 30, 30);
    }

    [self.titleLab addAttributes:@{NSForegroundColorAttributeName : [UIColor redColor]} forString:@"*"];
}

- (UIView *)maskView {
    if (!_maskView) {
        _maskView = [[UIView alloc] init];
        _maskView.backgroundColor = UIColor.redColor;
        [self.contentView addSubview:_maskView];
    }
    return _maskView;
}


- (void)configUI{
    

    self.titleLab = [[UILabel alloc] initWithFrame:CGRectMake(15, 5, 200, 30)];
    self.titleLab.font = Fond16;
    self.titleLab.textColor = Color333;
    [self.contentView addSubview:self.titleLab];
    
    
    self.subLab = [[UILabel alloc] init];
    self.subLab.font = Fond12;
    self.subLab.textColor = [UIColor colorWithHexString:@"#FF001B"];
    [self.contentView addSubview:self.subLab];
   
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    
    flowLayout.itemSize = CGSizeMake(kScreenWidth/kItemCount,kItemHeight);
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flowLayout.minimumLineSpacing= 0;
    flowLayout.minimumInteritemSpacing = 0;
    
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = UIColor.whiteColor;
    [self.contentView addSubview:self.collectionView];
    
    self.collectionView.scrollEnabled = NO;
    
    [self.collectionView registerClass:[LXApplyPicCollectionCell class] forCellWithReuseIdentifier:NSStringFromClass([LXApplyPicCollectionCell class])];
    
    
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSource.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    LXApplyPicCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([LXApplyPicCollectionCell class]) forIndexPath:indexPath];
    
    
 
    if (indexPath.item == self.dataSource.count - 1) {
//        cell.imgV.image = [UIImage imageNamed:@"guanYu"];
//        cell.imgV.contentMode = UIViewContentModeScaleAspectFit;
        cell.imgV.image = nil;
        cell.imgV.backgroundColor = [UIColor colorWithHexString:@"#F5F5F5"];
        cell.takeImgV.hidden = NO;
        NSDictionary *object = @{@"cell":cell,@"type":@1};
        [self performSelector:@selector(setLine:) withObject:object afterDelay:0.1];
       

    }
    else {
        NSDictionary *dic = self.dataSource[indexPath.item];
        [cell.imgV sd_setImageWithURL:[NSURL URLWithString:dic[@"url"]] placeholderImage:Image(@"default_icon")];
        cell.takeImgV.hidden = YES;
        cell.imgV.hidden = NO;
        cell.imgV.backgroundColor = [UIColor colorWithHexString:@"#F5F5F5"];
        NSDictionary *object = @{@"cell":cell,@"type":@2};
        [self performSelector:@selector(setLine:) withObject:object afterDelay:0.1];
    }
    
    if (indexPath.item == kNum) {
        cell.imgV.hidden = YES;
        cell.takeImgV.hidden = YES;
    }
    else {
//        cell.imgV.hidden = NO;
//        cell.takeImgV.hidden = NO;
    }
   
   if (indexPath.item == self.dataSource.count - 1) {
       cell.deleteImg.hidden = YES;
   }else{
       cell.deleteImg.hidden = NO;
   }

    
    WEAKSELF
    //选照片
    cell.tapImgCell = ^{
        if (indexPath.item == self.dataSource.count - 1) {
            if (weakSelf.callBackTakeAnnex) {
                weakSelf.callBackTakeAnnex(indexPath.item);
            }
        }
    };
    
    //删除
    cell.deleteImgBlock = ^{
        
        if (weakSelf.callBackDeleteAnnex) {
            weakSelf.callBackDeleteAnnex(indexPath.item);
        }
    };
    
    //放大
    cell.extendImgBlock = ^(id  _Nonnull obj) {
        if (indexPath.item != self.dataSource.count - 1) {
            if (weakSelf.callBackExtendAnnex) {
                weakSelf.callBackExtendAnnex(indexPath.item,obj);
            }
        }
    };
    
    return cell;
    
}

-(void)setLine:(NSDictionary *)dic{
    LXApplyPicCollectionCell *cell = dic[@"cell"];
    NSInteger type = [dic[@"type"] integerValue];
    if (type == 1) {
        [cell.imgV setLineDashPattern:@[@4,@2] withColor:[UIColor colorWithHexString:@"#C4C4C4"] withCornerRadius:5];
    }
    // 已经上传过的图片
    else if (type == 2) {
        
        [cell.imgV setLineDashPattern:@[@0,@0] withColor:[UIColor clearColor] withCornerRadius:5];
    }
   

}


@end
